//
//  SearchResultCell.m
//  ProjectsOne
//
//  Created by iOS开发 on 2018/7/11.
//  Copyright © 2018年 iOS开发. All rights reserved.
//

#import "SearchResultCell.h"

@interface SearchResultCell ()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UIImageView *picImageView;
@end

@implementation SearchResultCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    UIView *line = [[UIView alloc] init];
    line.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    [self addSubview:line];
    
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(self);
        make.height.mas_equalTo(.5f);
        make.left.mas_equalTo(self).with.offset(10);
    }];
    
    _picImageView = [[UIImageView alloc] init];
    _picImageView.backgroundColor = [UIColor yellowColor];
    [_picImageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
    _picImageView.contentMode =  UIViewContentModeScaleAspectFill;
    _picImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    _picImageView.clipsToBounds  = YES;
    _picImageView.hidden = YES;
    [self addSubview:_picImageView];
    
    [_picImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.left.mas_equalTo(self).with.offset(15);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(65);
    }];
    
    _headImageView = [[UIImageView alloc] init];
    _headImageView.backgroundColor = [UIColor redColor];
    [_headImageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
    _headImageView.contentMode =  UIViewContentModeScaleAspectFill;
    _headImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    _headImageView.clipsToBounds  = YES;
    _headImageView.layer.cornerRadius = 30;
    _headImageView.hidden = YES;
    [self addSubview:_headImageView];
    
    [_headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self);
        make.left.mas_equalTo(self).with.offset(15);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(60);
    }];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = [UIFont systemFontOfSize:17];
    _titleLabel.numberOfLines = 1;
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    _titleLabel.textColor = [UIColor colorWithHexString:@"333333"];
    [self addSubview:_titleLabel];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.picImageView.mas_right).with.offset(15);
        make.right.mas_equalTo(self).with.offset(-15);
        make.top.mas_equalTo(self.picImageView);
    }];
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.font = [UIFont systemFontOfSize:14];
    _contentLabel.numberOfLines = 2;
    _contentLabel.textAlignment = NSTextAlignmentLeft;
    _contentLabel.textColor = [UIColor colorWithHexString:@"808080"];
    [self addSubview:_contentLabel];
    
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.picImageView.mas_right).with.offset(15);
        make.right.mas_equalTo(self).with.offset(-15);
        make.top.mas_equalTo(self.titleLabel.mas_baseline).with.offset(15);
    }];
}

- (void)setModel:(SearchResultModel *)model {
    _model = model;
    
    _titleLabel.text = [NSString stringWithFormat:@"%@", model.title];
    _contentLabel.attributedText = model.attrContent;
    _contentLabel.lineBreakMode = NSLineBreakByTruncatingTail;
}

- (void)setCellType:(UITableViewCellType)cellType {
    _cellType = cellType;
    if (_cellType == UITableViewCellHead) {
        _headImageView.hidden = NO;
        _picImageView.hidden = YES;
    } else {
        _headImageView.hidden = YES;
        _picImageView.hidden = NO;
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
